<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>

<script>
  // module01
  let Light = function () {
    this.state = "off";
    this.button = null;
  }

  Light.prototype.init = function () {
    let button = document.createElement("button");
    let self = this;

    button.innerHTML = "开关";
    this.button = document.body.appendChild(button);
    this.button.onclick = function () {
      self.buttonWasPressed();
    }
  }

  Light.prototype.buttonWasPressed = function () {
    if (this.state == "off") {
      console.log("开灯");
      this.state = "on";
    } else if (this.state == "on") {
      console.log("关灯");
      this.state = "off";
    }
  }

  let light = new Light();
  light.init();
</script>

</html>